package com.doubao.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doubao.entity.SmartDevice;
import com.doubao.entity.SmartDeviceTag;
import com.doubao.entity.dto.SmartDeviceCreateDTO;
import com.doubao.entity.vo.SmartDeviceVO;
import com.doubao.response.Result;
import com.doubao.service.MinioService;
import com.doubao.service.SmartDeviceService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/api/device")
@RequiredArgsConstructor
public class SmartDeviceController {

    private final SmartDeviceService smartDeviceService;

    private final MinioService minioService;

    /**
     * 上传图标
     */
    @PostMapping("/upload/icon")
    public Result<String> uploadIcon(
            @RequestHeader(value = "User-Id") Long userId,
            @RequestParam("file") MultipartFile file) {
        String fileUrl = minioService.uploadFile(file, "device");
        return Result.success(fileUrl);
    }

    /**
     * 创建智能体
     */
    @PostMapping
    public Result<SmartDevice> createDevice(
            @RequestHeader(value = "User-Id") Long userId,
            @RequestBody SmartDeviceCreateDTO dto) {
        return Result.success(smartDeviceService.createDevice(dto, userId));
    }

    @GetMapping("/list")
    public Result<List<SmartDeviceVO>> listUserDevices(@RequestHeader(value = "User-Id") Long userId) {
        return Result.success(smartDeviceService.listUserDevices(userId));
    }

    @GetMapping("/public")
    public Result<IPage<SmartDeviceVO>> pagePublicDevices(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String category) {
        Page<SmartDevice> page = new Page<>(current, size);
        return Result.success(smartDeviceService.pagePublicDevices(page, category));
    }

    @PostMapping("/{deviceId}/collect")
    public Result<Void> collectDevice(
            @RequestHeader(value = "User-Id") Long userId,  // 改为 @RequestHeader
            @PathVariable Long deviceId) {
        smartDeviceService.collectDevice(userId, deviceId);
        return Result.success();
    }

    @DeleteMapping("/{deviceId}/collect")
    public Result<Void> uncollectDevice(
            @RequestHeader(value = "User-Id") Long userId,  // 改为 @RequestHeader
            @PathVariable Long deviceId) {
        smartDeviceService.uncollectDevice(userId, deviceId);
        return Result.success();
    }

    @PutMapping("/{deviceId}/tags")
    public Result<Void> updateTags(
            @PathVariable Long deviceId,
            @RequestBody List<String> tagNames) {
        smartDeviceService.updateDeviceTags(deviceId, tagNames);
        return Result.success();
    }

    @GetMapping("/{deviceId}/tags")
    public Result<List<String>> getTags(@PathVariable Long deviceId) {
        return Result.success(smartDeviceService.getDeviceTags(deviceId));
    }

    @GetMapping("/tags/hot")
    public Result<List<SmartDeviceTag>> getHotTags(
            @RequestParam(defaultValue = "10") Integer limit) {
        return Result.success(smartDeviceService.getHotTags(limit));
    }
}

